/**
 * Copyright (C) 2008 aileron.cc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cc.aileron.jww.admin;

import static cc.aileron.wsgi.WsgiHttpMethod.GET;
import static cc.aileron.wsgi.WsgiHttpMethod.POST;
import static cc.aileron.wsgi.container.utility.WsgiProcessRegistry.registry;
import cc.aileron.commons.resource.ResourceNotFoundException;
import cc.aileron.jww.admin.model.dto.Auth;
import cc.aileron.jww.admin.view.judgment.AuthContinuanceJudgment;
import cc.aileron.jww.admin.view.judgment.AuthJudgment;
import cc.aileron.jww.admin.view.process.MenuProcess;
import cc.aileron.jww.admin.view.resources.PageResource;
import cc.aileron.template.exception.CompileException;
import cc.aileron.wsgi.WsgiProcess;
import cc.aileron.wsgi.WsgiController;
import cc.aileron.wsgi.container.WsgiContainer;
import cc.aileron.wsgi.context.WsgiApplicationContext;
import cc.aileron.wsgi.provider.WsgiRedirectProvider;
import cc.aileron.wsgi.provider.WsgiStaticRedirectProvider;
import cc.aileron.wsgi.provider.WsgiTemplateProvider;

import com.google.inject.Inject;

/**
 * wsgi-module
 * @author Aileron
 *
 */
public class JwwAdminController implements WsgiController
{
    /* (非 Javadoc)
     * @see cc.aileron.wsgi.WsgiSetting#setting(cc.aileron.wsgi.container.WsgiContainer)
     */
    @Override
    public void setting(final WsgiContainer container)
    {
        /**
         * 認証
         */
        registry(Auth.class, "account", "password")
            .method(POST).uri("auth")
            .judgment(auth, authError)
            .add(redirectMenu)
            .register(container);

        /**
         * 画面表示
         */
        registry(PageResource.class)
            .method(GET).uri("menu/#{selectItemId}/content.html")
            .judgment(authContinuance, authError)
            .add(menu)
            .add(page)
            .register(container);
    }
    
    @Inject
    public JwwAdminController(
            final WsgiStaticRedirectProvider staticRedirectProvider,
            final WsgiRedirectProvider redirectProvider,
            final WsgiTemplateProvider templateProvider,
            final AuthJudgment auth,
            final AuthContinuanceJudgment authContinuance,
            final MenuProcess menu,
            final WsgiApplicationContext context) throws CompileException, ResourceNotFoundException
    {
        this.auth = auth ;
        this.authContinuance = authContinuance;
        this.authError = templateProvider.get("WEB-INF/pages/auth-error.html", contentType);
        this.redirectMenu = staticRedirectProvider.get(context.getContextPath() + "menu.html");
        this.menu = menu;
        this.page = templateProvider.get("WEB-INF/pages/page.html", contentType);
    }
    
    /**
     * contentType
     */
    private final String contentType = "text/html; charset=UTF-8";
    
    /**
     * 認証
     */
    private final AuthJudgment auth;
    
    /**
     * 認証の継続
     */
    private final AuthContinuanceJudgment authContinuance;
    
    /**
     * auth-error
     */
    private final WsgiProcess<Object> authError ;
    
    /**
     * redirect-menu
     */
    private final WsgiProcess<Object> redirectMenu ;
    
    /**
     * menu 表示
     */
    private final MenuProcess menu;
    
    /**
     * 画面リソース
     */
    private final WsgiProcess<PageResource> page ;
}